﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Xpom_Xpum__Code_Generator.Utilities;

namespace Xpom_Xpum__Code_Generator
{
    public static class Facade
    {
        static Subsystems.CommandLineManager CommandLineManagerObject = new Subsystems.CommandLineManager();
        static Subsystems.ProjectManager ProjectManagerObject = new Subsystems.ProjectManager();

        private static bool IsInited { get; set; }

        private static string Context = "";

        public static void ProcessInit(string[] args)
        {
            if (!Facade.IsInited)
            {
                CommandLineManagerObject.CommandLineArgs = args;

                if (CommandLineManagerObject.CheckForEnoughCommandLineArgs(2))
                {
                    if (GlobalVariables.SetSDKPath(args[1]))
                    {
                        Console.WriteLine("Success - inited");

                        IsInited = true;

                        while (true)
                        {
                            Console.WriteLine();
                            Console.Write(Context + "> ");

                            Read();
                        }
                    }
                }
            }
            else
            {
                Console.WriteLine("Error - Already inited");
            }
        }

        public static void Read()
        {
            var input = Console.ReadLine();
            var inputParts = StringParser.QuoteSplit(input);
            /*for (int i = 0; i < inputParts.Length; i++)
            {
                Console.WriteLine(inputParts[i] + "|");
                Console.WriteLine("---");
            }
            return;
             */
            
            if (inputParts.Length >= 1)
            {
                switch (inputParts[0])
                {
                    case "add":
                        ProjectManagerObject.AddProperty(inputParts[1]);
                        break;
                    case "create":
                        if (inputParts.Length >= 3)
                        {
                            var path = inputParts[1];
                            var template = inputParts[2];
                            ProjectManagerObject.Create(path, template);
                            Console.WriteLine("Success - created");
                            Context = "PROJECT";
                        }
                        else
                        {
                            Console.WriteLine("Error - no ext path and/or template name defined");
                        }
                        break;

                    case "get":
                        Console.WriteLine(ProjectManagerObject.GetProperty(inputParts[1]));
                        break;
                    case "open":
                        if (inputParts[1] == "manifest")
                        {
                            Context = "MANIFEST";
                        }
                        break;
                    case "save":
                        if (ProjectManagerObject != null)
                        {
                            ProjectManagerObject.Save();
                        }
                        else
                        {
                            Console.WriteLine("Error - create project first");
                        }
                        break;
                    case "set":
                        ProjectManagerObject.SetProperty(inputParts[1], inputParts[2]);
                        break;
                    case "remove":
                        ProjectManagerObject.RemoveProperty(inputParts[1]);
                        break;
                    default:
                        break;
                }
            }
        }
    }
}